package com.siemens.industrial.app;

import com.siemens.industrial.model.JobType;
import org.quartz.SchedulerException;

import java.io.*;
import java.net.Socket;
import java.util.Arrays;

/**
 * Created by David on 2016/11/16.
 */
public class SocketHandler extends Thread {
    private Socket socket = null;
    private BufferedReader br = null;
    private PrintWriter pw = null;

    public SocketHandler(Socket socket) {
        this.socket = socket;
        try {
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                    socket.getOutputStream())));
            start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            if (!socket.getInetAddress().getHostAddress().equals("127.0.0.1")) {
                pw.print("{\"code\":403, \"message\":\"Unauthorized\"}");
            } else {
                String msg = br.readLine();
                System.out.println(msg);

                if (msg.equals(JobType.DB_Backup_Plan_Update.toString())) {
                    try {
                        DBBackupTask.INSTANCE.scheduler();
                    } catch (SchedulerException e) {
                        pw.print("{\"code\":404, \"message\":\"resume job failed!\"}");
                        pw.flush();
                    }
                } else if (msg.equals(JobType.DB_Backup_Execute.toString())) {
                    DBBackupImplementation db = new DBBackupImplementation();
                    db.fullBackup();
                } else if (msg.equals(JobType.Event.toString())) {
                    String info = br.readLine();
                    EventImplementation event = new EventImplementation();
                    event.sendMail(info);
                } else if(msg.equals(JobType.Device_Status_Dispose.toString())){
                    String info = br.readLine();
                    DeviceStatusImplementation deviceStatus = new DeviceStatusImplementation();
                    deviceStatus.deviceStatusDispose(info);
                } else if(msg.equals(JobType.Person_Position_Dispose.toString())){
                    String info = br.readLine();
                    DeviceStatusImplementation deviceStatus = new DeviceStatusImplementation();
                    deviceStatus.deviceStatusDispose(info);
                } else if(msg.equals(JobType.Blanking_position_Dispose.toString())){
                    String info = br.readLine();
                    DeviceStatusImplementation deviceStatus = new DeviceStatusImplementation();
                    deviceStatus.deviceStatusDispose(info);
                }
                pw.print(200);
                pw.flush();
            }
        } catch (IOException e) {
            pw.print("{\"code\":404, \"message\":\"" + e.getMessage() + "\"}");
            pw.flush();
            close();
        } finally {
            close();
        }
    }

    public void close() {
        try {
            br.close();
            pw.close();
            socket.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            System.out.println(Arrays.toString(e.getStackTrace()));
        }
    }
}
